package com.byt.common.adapter.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.byt.base.utils.onclicktwo.AntiShake;
import com.byt.common.R;
import com.byt.common.activity.BigImagePagerActivity;
import com.byt.common.image.ImageLoaderUtils;

import java.util.List;

public class CommonImageAdapter extends BaseAdapter {
    private List<String> listData;
    private Context mContext;

    /**
     * @param imgList
     * @param mContext
     */
    public CommonImageAdapter(List<String> imgList, Context mContext) {
        this.listData = imgList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int listData) {
        return listData;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Hodler hodler;
        if (convertView == null) {
            hodler = new Hodler();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_item_change_list, null);
            hodler.imageView = convertView.findViewById(R.id.img_change_list_item);
            hodler.tv_pic_num = convertView.findViewById(R.id.tv_pic_num);
            convertView.setTag(hodler);
        } else {
            hodler = (Hodler) convertView.getTag();
        }
        RelativeLayout.LayoutParams params;
        if (getCount() == 1 || getCount() == 2 || getCount() == 4) {
            params = new RelativeLayout.LayoutParams((int) mContext.getResources().getDimension(R.dimen.x485),
                    (int) mContext.getResources().getDimension(R.dimen.y485));
        } else {
            params = new RelativeLayout.LayoutParams((int) mContext.getResources().getDimension(R.dimen.x320),
                    (int) mContext.getResources().getDimension(R.dimen.y320));
        }
        hodler.tv_pic_num.setVisibility(View.GONE);
        hodler.imageView.setLayoutParams(params);
        ImageLoaderUtils.display(hodler.imageView, listData.get(position));
        hodler.imageView.setOnClickListener(v -> {
            if (AntiShake.check(v.getId())) {
                return;
            }
            BigImagePagerActivity.startImagePagerActivity(mContext, listData, position);
        });

        return convertView;
    }

    class Hodler {
        ImageView imageView;
        TextView tv_pic_num;
    }
}
